<?php
/**
 * Created by PhpStorm.
 * User: chenx
 * Date: 18-3-25
 * Time: 下午9:50
 */

namespace common\service;


use common\components\HttpClient;
use common\models\Code;

class SmsService extends BaseService
{
    /**
     * 短信验证码入库
     * @param string $phone
     * @return string
     */
    public static function addSms($phone){
        $code = CommonService::random(4,1);
//        $code = '1234';
        $row['phone'] = $phone;
        $row['code'] = $code;
        $row['time'] = time();
        $row['status'] = 0;
        $row['client_ip'] = CommonService::getIP();
        $row['is_del'] = 0;
        $code_model = new Code();
        $code_model->setAttributes($row);
        $code_model->save();
        return $code;
    }

    /**
     * 发送短信验证码
     * @param string $mobile
     * @param $m_code
     * @return bool
     */
    public static function sendSms($phone='', $m_code){
        //欧一短信品台
//        if(!$phone) return false;
//        $message = '【拼团e】提醒您，您的验证码是:'.$m_code.',验证码在5分钟后过期,请您及时验证，工作人员不会向您索要验证码，请勿向任何单位或个人泄露！';
//        $data = [
//            'un' => \Yii::$app->params['phone_code']['user'],
//            'pw' => \Yii::$app->params['phone_code']['pwd'],
//            'da' => $phone,
//            'sm' => bin2hex(iconv("UTF-8", "GB2312", $message)),
//            'dc' => 15,
//            'rd' => 0
//        ];
//        $resp = HttpClient::post(\Yii::$app->params['phone_code']['url'],$data);
//
//        if (explode('=', $resp)[0] === 'id') return true;
//        return false;

        //千向短信品台
        if(empty($m_code) || empty($phone)) return false;
        $sing = \Yii::$app->params['phone_sing'];
        $message = '【'.$sing.'】提醒您，您的验证码是:'.$m_code.',验证码在5分钟后过期,请您及时验证，工作人员不会向您索要验证码，请勿向任何单位或个人泄露！';

        $data = [
            'username' => \Yii::$app->params['phone_code']['qx']['user'],
            'passwd' =>\Yii::$app->params['phone_code']['qx']['pwd'],
            'phone' => $phone,
            'msg' => $message,
            'needstatus' => 'true',
        ];
        $resp = HttpClient::post(\Yii::$app->params['phone_code']['qx']['url'],$data);
        $resp = json_decode($resp);
        if($resp->respcode==0){
            return true;
        }else{
            return false;
//            echo "发送失败，失败原因：".$resp->respdesc;
        }




    }

}